package pl.edu.mimuw.irs.rcp.controller.editor.util;

import org.eclipse.gef.EditDomain;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.ui.properties.UndoablePropertySheetEntry;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.PropertySheetPage;

public abstract class AbstractAdaptableDataWrapper<T> implements IAdaptableDataWrapper<T> {

	// *** Attributes

	private T data;
	private EditDomain editDomain;
	private PropertySheetPage propertySheetPage;
	@Override
	public T getData() {
		return this.data;
	}
	public void setData(T newData) {
		this.data = newData;
	}
	public EditDomain getEditDomain() {
		return editDomain;
	}
	public void setEditDomain(EditDomain editDomain) {
		this.editDomain = editDomain;
	}
	public CommandStack getCommandStack() {
		return getEditDomain().getCommandStack();
	}
	public PropertySheetPage getPropertySheetPage() {
		if (propertySheetPage == null) {
			propertySheetPage = new PropertySheetPage();
			propertySheetPage.setRootEntry(
					new UndoablePropertySheetEntry(getCommandStack()));
		}
		return propertySheetPage;
	}
	
	// *** Constructors
	
	public AbstractAdaptableDataWrapper(T data, EditDomain editDomain) {
		setData(data);
		setEditDomain(editDomain);
	}
	
	// *** Methods
	
	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySheetPage.class)
			return getPropertySheetPage();
		return null;
	}
}
